package com.serviceprovider.business.module.report.valueobjects;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.sp.transaction.model.Batch;
import com.serviceprovider.business.module.sp.transaction.model.TransactionUserAmount;
import com.serviceprovider.business.module.user.model.impl.User;

//~--- JDK imports ------------------------------------------------------------

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class TransactionsReportData {
    private List<User>                     allPayees = new ArrayList<User>();
    private List<TransactionsReportRecord> records   = new ArrayList<TransactionsReportRecord>();
    private Batch                          batch;
    private Integer                        selfFeeTotals;
    private int                            totalAmount;
    private double                         totalServiceFee;

    public TransactionsReportData() {}

    public List<TransactionsReportRecord> getRecords() {
        return records;
    }

    public void setRecords(List<TransactionsReportRecord> records) {
        this.records = records;
        updatePayeesList();
    }

    private void updatePayeesList() {
        allPayees.clear();

        Set<Integer> addedUser = new HashSet<Integer>();

        for (TransactionsReportRecord trans : records) {
            for (TransactionUserAmount transUserAmount : trans.getTransactionUserAmounts()) {
                if (!addedUser.contains(transUserAmount.getUserId())) {
                    addedUser.add(transUserAmount.getUserId());
                    allPayees.add(transUserAmount.getUser());
                }
            }
        }

        Collections.sort(allPayees, new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {
                return o1.getFullName().compareTo(o2.getFullName());
            }
        });
    }

    public Integer getSelfFeeTotals() {
        return selfFeeTotals;
    }

    public List<User> getAllPayees() {
        return allPayees;
    }

    public void setSelfFeeTotals(Integer selfFeeTotals) {
        this.selfFeeTotals = selfFeeTotals;
    }

    public void setTotalAmount(int totalAmount) {
        this.totalAmount = totalAmount;
    }

    public int getTotalAmount() {
        return totalAmount;
    }

    public void setBatch(Batch batch) {
        this.batch = batch;
    }

    public Batch getBatch() {
        return batch;
    }

    public void setTotalServiceFee(double totalServiceFee) {
        this.totalServiceFee = totalServiceFee;
    }

    public double getTotalServiceFee() {
        return totalServiceFee;
    }

    public List<Double> getTotalPayeeAmounts() {
        List<Double> amounts = new ArrayList<Double>();

        for (User user : allPayees) {
            Double amount = 0.0;

            for (TransactionsReportRecord record : records) {
                amount += (record.getAmountOfUser().get(user.getId()) != null)
                          ? record.getAmountOfUser().get(user.getId())
                          : 0.0;
            }

            amounts.add(amount);
        }

        return amounts;
    }

    public int getNumberOfPayees() {
        return allPayees.size();
    }

    public double getTotalAll() {
        double result = 0.0;

        for (TransactionsReportRecord record : records) {
            result += record.getTotalAll();
        }

        return result;
    }

    public double getTotalCustomerBalance() {
        double result = 0.0;

        for (TransactionsReportRecord record : records) {
            result += record.getCustomerBalance();
        }

        return result;
    }
}



